import type { CrudSchema } from '@/hooks/web/useCrudSchemas'
import { dateFormatter } from '@/utils/formatTime'
import { TableColumn } from '@/types/table'
import { KnowledgeCateApi, KnowledgeCateVO } from '@/api/exam/knowledgecate'
import { handleTree } from '@/utils/tree'
import { KnowledgeVO } from '@/api/exam/knowledge'
// 表单校验
export const rules = reactive({
                        title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
                        content: [{ required: true, message: '内容 不能为空', trigger: 'blur' }]
})
export const knowledgeCateTree = ref<Tree[]>([]) // 树形结构
export const knowledgeCateList = ref<KnowledgeCateVO[]>([]) // 知识点分类列表
export const getKnowledgeCateTree = async () => {
  knowledgeCateTree.value = []
  const data = await KnowledgeCateApi.getKnowledgeCateList({ type: 1 })
  knowledgeCateList.value = data
  // const root: Tree = { id: 0, name: '选择知识点分类', children: [] }
  knowledgeCateTree.value = handleTree(data, 'id', 'pid')
  // knowledgeCateTree.value.push(root)
}
await getKnowledgeCateTree()
//复杂配置参照文档修改
const crudSchemas = reactive<CrudSchema[]>([
  {
    label: 'ID',
    field: 'id',
    isSearch:  false ,
	isColumnHidden:  false 
  },
  {
    label: '分类',
    field: 'category',
    isSearch:  true ,
	isColumnHidden:  false ,
  formatter: (row: KnowledgeVO) => {
    // return "sss";
    return knowledgeCateList.value.find(item => item.id == Number(row.category))?.name
  },
	search: { 
      component: 'TreeSelect',
      componentProps: {
        data: knowledgeCateTree.value,
        checkStrictly: true,
        defaultExpandAll: true,
        props: {
          label: 'name',
          value: 'id'
        },
        style: {
          width: '240px'
        }
      },
    }
  },
  {
    label: '标题',
    field: 'title',
    isSearch:  true ,
	isColumnHidden:  false ,
	search: {
      componentProps: {
        style: {
          width: '240px'
        }
      }
    }
  },
  {
    label: '内容 ',
    field: 'content',
    isSearch:  false ,
	isColumnHidden:  true 
  },
  {
    label: '创建者',
    field: 'creator',
    isSearch:  false ,
	isColumnHidden:  true 
  },
  {
    label: '创建时间',
    field: 'createTime',
    isSearch:  true ,
	isColumnHidden:  false ,
	formatter: dateFormatter,
	search: {
		component: 'DatePicker',
      componentProps: {
		  valueFormat: 'YYYY-MM-DD HH:mm:ss',
        style: {
          width: '240px'
        }
      }
    }
  },
  {
    label: '更新者',
    field: 'updater',
    isSearch:  false ,
	isColumnHidden:  true 
  },
  {
    label: '更新时间',
    field: 'updateTime',
    isSearch:  false ,
	isColumnHidden:  true ,
	formatter: dateFormatter
  },
  {
    label: '是否删除',
    field: 'deleted',
    isSearch:  false ,
	isColumnHidden:  true 
  },
  {
    label: '租户编号',
    field: 'tenantId',
    isSearch:  false ,
	isColumnHidden:  true 
  },
  {
    label: '操作',
    field: 'action',
    isForm: false
  }
])
export const { allSchemas } = useCrudSchemas(crudSchemas)